<template>
  <el-dialog :title="title" :visible="isShow" width="630px" @close="closeFn">
    <el-form ref="addForm" :rules="rules" :model="addForm">
      <el-form-item label="型号名称：" prop="name" label-width="150px">
        <el-input
          style="width: 85%"
          v-model="addForm.name"
          placeholder="请输入"
          maxlength="10"
          show-word-limit
          autocomplete="off"
        ></el-input>
      </el-form-item>
      <el-form-item label="型号编码:" label-width="150px" prop="model">
        <el-input
          style="width: 85%"
          v-model="addForm.model"
          placeholder="请输入(限制数字、字母、中划线、下划线)"
          maxlength="15"
          show-word-limit
          autocomplete="off"
        ></el-input>
      </el-form-item>
      <el-form-item label="货道行数:" label-width="150px" prop="vmRow">
        <el-input-number
          style="width: 85%"
          v-model="addForm.vmRow"
          controls-position="right"
          placeholder="请输入"
          :min="0"
          :max="10"
        ></el-input-number>
      </el-form-item>
      <el-form-item label="货道列数:" label-width="150px" prop="vmCol">
        <el-input-number
          style="width: 85%"
          v-model="addForm.vmCol"
          controls-position="right"
          placeholder="请输入"
          :min="0"
          :max="10"
        ></el-input-number>
      </el-form-item>
      <el-form-item
        label="货道容量:"
        label-width="150px"
        prop="channelMaxCapacity"
      >
        <el-input-number
          style="width: 85%"
          v-model="addForm.channelMaxCapacity"
          controls-position="right"
          placeholder="请输入"
          :min="0"
          :max="10"
        ></el-input-number>
      </el-form-item>
      <el-form-item label="设备图片:" label-width="150px">
        <!-- 上传图片 -->
        <importImg v-model="addForm.image" />
      </el-form-item>
      <el-row type="flex" justify="center">
        <el-col :span="8">
          <el-button
            class="btn cancel"
            @click="closeFn"
            style="margin-right: 20px"
            >取消</el-button
          >
          <el-button class="btn confirm" type="primary" @click="submit"
            >确认</el-button
          >
        </el-col>
      </el-row>
    </el-form>
  </el-dialog>
</template>

<script>
import importImg from './importImg.vue';
import { addVmTypeApi, editVmTypeApi } from '@/api/vm'
export default {
  components:{
    importImg
  },
  props: {
    title: String,
    isShow: Boolean,
    typeNow: Object
  },
  data() {
    return {
      addForm: {
        channelMaxCapacity: "",
        model: "",
        image: "",
        vmRow: "",
        vmCol: "",
        name: "",
      },
      imageUrl: "",
      pageParams: {
        pageIndex: 1,
        pageSize: 1000,
      },
      rules: {
        name: [{ required: true, message: "请输入型号名称", trigger: "blur" }],
        model: [{ required: true, message: "请输入型号编码", trigger: "blur" }],
        vmRow: [{ required: true, message: "请输入货道行数", trigger: "blur" }],
        vmCol: [{ required: true, message: "请输入货道列数", trigger: "blur" }],
        channelMaxCapacity: [
          { required: true, message: "请输入货道容量", trigger: "blur" },
        ],
        image: [
          { required: true, message: "请上传设备图片", trigger: "change" },
        ],
      },
    };
  },
  created() {
    if(this.title === '修改设备类型'){
      this.addForm = this.typeNow
    }
  },
  methods: {
   
    closeFn() {
      this.$refs.addForm.resetFields();
      this.addForm = {
        channelMaxCapacity: "",
        model: "",
        image: "",
        vmRow: "",
        vmCol: "",
        name: "",
      };
      this.$emit("update:isShow", false);
    },
    submit() {
      this.$refs.addForm.validate(async (pass) => {
        if (pass) {
          if(this.title === '新增设备类型'){
            await addVmTypeApi(this.addForm);
            this.$message.success("添加设备类型成功");
          } else {
            await editVmTypeApi(this.typeNow.id,this.addForm)
            this.$message.success("修改设备类型成功");
          }
          this.closeFn();
          this.$emit("searchVmType");
        }
      });
    },
  },
};
</script>

<style lang="scss" scoped>
.btn {
  width: 80px;
  height: 36px;
  border: none;
  padding: 0;
  line-height: 36px;
}
.cancel {
  background-color: #fbf4f0;
  color: #7e7570;
}
.confirm {
  background-color: #ff6d29;
  color: #fff;
}

.avatar-uploader .el-upload {
  border: 1px dashed #d9d9d9;
  border-radius: 6px;
  cursor: pointer;
  position: relative;
  overflow: hidden;
}
.avatar-uploader .el-upload:hover {
  border-color: #409eff;
}
.avatar-uploader-icon {
  width: 84px;
  height: 84px;
  line-height: 84px;
  text-align: center;
  color: #bac0cd;
  background-color: #f3f6fb;
  border-radius: 6px;
  border: 1px dashed #d9d9d9;

}
.avatar {
  width: 84px;
  height: 84px;
  display: block;
}
</style>
